/**
 * 活动详情
 * @author Qasim <15750783791@163.com>
 * @version 2016/9/5
 */
var self = phone = id = uid = tid =  company_name = classify = business_id = details = '';  

document.addEventListener('plusready' , ready ,  false);

function ready(){
	
	self = plus.webview.currentWebview();
	
	var memberInfo = plus.storage.getItem('memberInfo');
	
	memberInfo = $.parseJSON(memberInfo);
	
	uid = memberInfo.aid;
	
	console.log(self.aid)
	
	$('.title').text(self.title);
//	console.log(self.title) 
	details = self.body.indexOf('src="http') >= 0 ? self.body : self.body.replace(/src="/g , 'src="' + webRoot);
	$('.bbs-inf-text').html(details)
	 
	id = self.aid; 
	 
	tid = self.tid;
	//留言
	$('.ly').on('click' , function(){
		var ly = plus.webview.create('ly.html', 'ly', {}, {
			uid : memberInfo.aid, 
			nickname : memberInfo.nickname,
			aid : id,
			title : self.title,   
		}); 
		ly.addEventListener('loaded', function (){
			ly.show('pop-in');
		})	
	})
 	//浏览量与收藏状态
 	plus.nativeUI.showWaiting('加载中...');
 	$.ajax({  
		type:"post", 
		url:apiRoot + '/Home/Article/collection_status/',
		data:{
			uid : uid ,  
			aid : id ,
			tid : tid,
			evaluate : 'evaluate' 
		},
		 
		dataType : 'json',
		
		success : function(data){ 
			
			plus.nativeUI.closeWaiting();
			
			if(data.business != ''){
				
				phone = data.business.phone;
				company_name = data.business.title;
				classify = data.business.classify;
				business_id = data.business.id;
			}
			
			if(data.status)$('.collection_status').css('color' , 'darkgoldenrod');
			if(data.message != ''){
				$('.total_number').text(data.message.length);  
				$('.inf-pl').each(function(index){
					var self = $(this); 
					if(data.message[index]){
						if(data.message[index].level > 1 ) $('.nav-wjx').find('span').eq(1).removeClass('color-c')
						if(data.message[index].level > 2 ) $('.nav-wjx').find('span').eq(2).removeClass('color-c')
						if(data.message[index].level > 3 ) $('.nav-wjx').find('span').eq(3).removeClass('color-c')
						if(data.message[index].level > 4 ) $('.nav-wjx').find('span').eq(4).removeClass('color-c')
						self.find('i img').attr('src' , getImgUrl(data.message[index].avatar));
						self.find('.name span').text(data.message[index].nickname);
						self.find('.font-11').text(data.message[index].addtime);
						self.find('.margin-top-5').text(data.message[index].body);
						if(data.message[index].pics){ 
							$.each(data.message[index].pics, function(k ,v) { 
								if(v)self.find('.img-div').append('<img src="'+getImgUrl(v)+'"alt="" data-preview-src="" data-preview-group="2"/>');
							});
						}  
					}else{
						$('.lists').children().eq(1).remove();
					}
					$('.lists').css('display' , 'block')
				})
			}else{
				$('.lists').html('')
			}
			
			$('.hits').text(data.hits); 
			
		},
		error : function(e){
			console.log(JSON.stringify(e))
			toast('连接超时,请检查网络');
			plus.nativeUI.closeWaiting();
		}
		
	});
   	//点击分享
	$('.share').each(function(index) { 
		var type = ex = content = ''  
		$(this).on('click', function() {
			if(index == 0) {type = 'weixin' ; ex = 'WXSceneSession'; content = '';} 
			if(index == 1) {type = 'qq' ; ex = ''; content = $('.content').text() ; } 
			if(index == 2) {type = 'weixin' ; ex = 'WXSceneTimeline';content = '';}
			shareAction(type, ex , id , '端砚生物' , '' , '' , uid , tid);
		})
	})
 	//拨打电话
 	$('.call').on('click' , function(){
 		if(!phone){
 			toast('没有获取到手机号');return;
 		}
 		plus.nativeUI.confirm('温馨提示: 	确定要拨打'+phone+'吗？' , function(e){
 			if(e.index == 0){
 				plus.device.dial(phone , false);
 			}
 		} , '第三方咨询' , ['确定' , '取消'])
 	}) 
 	
 	
 	//下单
 	$('.xiadan').on('tap' , function(){
	    plus.nativeUI.showWaiting('跳转中...');
 		var order_xd_inf = plus.webview.create('order-xd-inf.html', 'order_xd_inf', {}, {
			uid : memberInfo.aid, 
			nickname : memberInfo.nickname,
			aid : business_id,
			title : company_name,
			classify : details
		}); 
		order_xd_inf.addEventListener('loaded', function (){
			order_xd_inf.show('pop-in');
			plus.nativeUI.closeWaiting();
		})	
 	}) 
 	
 	
 	//收藏
 	$('.collection').on('click' , function(){
 		plus.nativeUI.showWaiting('操作中...'); 
 		$.ajax({  
 			type:"post", 
 			url:apiRoot + '/Home/Article/collection/',
 			data:{
 				uid : uid , 
 				aid : id ,
 				tid : tid
 			},
 			success : function(data){ 
 				plus.nativeUI.closeWaiting();  
 				var type = '';
 				if(data == 0){ 
 					toast('已取消');
 					type = '取消收藏';
 					$('.collection_status').css('color' , 'aliceblue');
 				}else{
 					toast('已收藏');
   					$('.collection_status').css('color' , 'darkgoldenrod');
 				}
 				
   				if(self.page){
   					mui.fire(plus.webview.getWebviewById(self.page) , 'cancel' ,{
	 					id : id  ,
	 					type : type
					});

   				}
 			},
 			error : function(e){
 				console.log(JSON.stringify(e))
 				toast('连接超时,请检查网络');
 				plus.nativeUI.closeWaiting();
 			}
 			
 		});
 	})
 	
 	
}
